#pragma once

#include <cstdio>
#include <string>
#include <iostream>
#if defined(__linux__)
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#elif defined(_WIN32) || defined(_WIN64)
#include "windows.h"
#endif

//该值表示用一时间能够接收多少客户端连接
//并非指整个通信最多接收多少客户端连接
#define MAX_LISTEN 5

class TcpSocket
{
public:
    TcpSocket();

    bool Socket();

    bool Bind(const std::string &ip, uint16_t port);

    bool Listen(int backlog = MAX_LISTEN);

    bool Accept(TcpSocket *new_sock, std::string *ip = nullptr, uint16_t *port = nullptr);

    bool Recv(std::string *buf, int flag);

    bool Send(const std::string &data);

    bool Close();

    bool Connect(const std::string &ip, uint16_t port);

private:
    int _sockfd;
};



